/*******************************************************************************
 * Copyright (c) 2011 - 2012 TXT e-solutions SpA
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * This work was performed within the IoT_at_Work Project
 * and partially funded by the European Commission's
 * 7th Framework Programme under the research area ICT-2009.1.3
 * Internet of Things and enterprise environments.
 *
 *
 * Authors:
 *     Cristoforo Seccia (TXT e-solutions SpA)
 *     Salvatore Piccione (TXT e-solutions SpA)
 *
 * Contributors:
 *      Domenico Rotondi (TXT e-solutions SpA)
 *******************************************************************************/
package it.txt.access.capability.revocation.wizard;

import it.txt.access.capability.factory.CapabilitySchemaFactory;
import it.txt.access.capability.factory.CapabilitySchemaFactoryException;
import it.txt.access.capability.revocation.wizard.panel.descriptor.DescriptorPanelAssertion;
import it.txt.access.capability.revocation.wizard.panel.descriptor.DescriptorPanelAuthorisingCapabilitySelection;
import it.txt.access.capability.revocation.wizard.panel.descriptor.DescriptorPanelKeystore;
import it.txt.access.capability.revocation.wizard.panel.descriptor.DescriptorPanelRevocationData;
import it.txt.access.capability.revocation.wizard.panel.descriptor.DescriptorPanelWelcome;

import java.io.File;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

/**
 * Graphical Wizard Application that allows users to create a root authorization
 * capability or a capability derived from a previously defined one. The 
 * application can be also used to verify the validity of a Capability.
 *
 * @version 1.0
 * @author  http://www.txtgroup.com
 * @author  Cristoforo Seccia (cristoforo.seccia@txtgroup.com)
 * @author  Salvatore Piccione (salvatore.piccione@network.txtgroup.com)
 * @contributor Domenico Rotondi (domenico.rotondi@txtgroup.com)
 */
public class WizardMainListener {

    private static final Logger LOGGER = Logger.getLogger(WizardMainListener.class.getName());
    private Wizard wizard;

    /**
     * Installs the JGoodies Look & Feels, if available, in classpath.
     */
    public final void initializeLookAndFeels() {
        // if in classpath thry to load JGoodies Plastic Look & Feel
        try {
            LookAndFeelInfo[] lnfs = UIManager.getInstalledLookAndFeels();
            boolean found = false;
            for (int i = 0; i < lnfs.length; i++) {
                if (lnfs[i].getName().equals("JGoodies Plastic 3D")) {
                    found = true;
                }
            }
            if (!found) {
                UIManager.installLookAndFeel("JGoodies Plastic 3D",
                        "com.jgoodies.looks.plastic.Plastic3DLookAndFeel");
            }
            UIManager.setLookAndFeel("com.jgoodies.looks.plastic.Plastic3DLookAndFeel");
            //UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");            
        } catch (Throwable t) {
            try {
                LOGGER.log(Level.INFO, "Error while setting L&F. Set the system default L&F.");
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void createAndShowWizard() throws WizardException {
        initializeLookAndFeels();
        
        wizard = new Wizard();
        wizard.setDefaultCloseOperation(Wizard.DO_NOTHING_ON_CLOSE);
        WizardPanelDescriptor[] descriptors = new WizardPanelDescriptor[5];
        int i = 0;
        descriptors[i] = new DescriptorPanelWelcome(wizard, i, descriptors.length);
        i++;
        descriptors[i] = new DescriptorPanelAuthorisingCapabilitySelection(wizard, i, descriptors.length);
        i++;
        descriptors[i] = new DescriptorPanelRevocationData(wizard, i, descriptors.length);
        i++;
        descriptors[i] = new DescriptorPanelKeystore(wizard, i, descriptors.length);
        i++;
        descriptors[i] = new DescriptorPanelAssertion(wizard, i, descriptors.length);
        wizard.setWizardPanelDescriptors(descriptors);
        wizard.pack();
        wizard.setVisible(true);
    }

    public static void main(String[] args) {
        String schemasPath = null;
        if (args.length < 1) {
            System.err.println("Usage: Capability Schemas Path.");
            System.exit(1);
        } else {
            schemasPath = new File(args[0]).getAbsolutePath();
        }
        System.out.println("Schemas path: " + schemasPath);
        Locale.setDefault(Locale.UK);
        WaitFrameController wfc = null;
        try {
            wfc = new WaitFrameController();
            wfc.displayApplicationLoadingFrame();
            CapabilitySchemaFactory.getInstance(schemasPath);
            
            if (wfc.isApplicationLoadingFrameVisible()) {
                wfc.closeApplicationLoadingFrame();
            }
            WizardMainListener listener = new WizardMainListener();
            listener.createAndShowWizard();
        } catch (CapabilitySchemaFactoryException ex) {
            if (wfc.isApplicationLoadingFrameVisible()) {
                wfc.closeApplicationLoadingFrame();
            }
            LOGGER.log(Level.SEVERE, null, ex);
            System.exit(-1);
        } catch (WizardException ex) {
            if (wfc.isApplicationLoadingFrameVisible()) {
                wfc.closeApplicationLoadingFrame();
            }
            LOGGER.log(Level.SEVERE, null, ex);
            System.exit(-1);
        } catch (RuntimeException re) {
            if (null != wfc && wfc.isApplicationLoadingFrameVisible()) {
                wfc.closeApplicationLoadingFrame();
            }
            LOGGER.log(Level.SEVERE, null, re);
            System.exit(-1);
        }
    }
}
